package com.example.test.chapter14;

import com.example.test.R;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MyNotepadB extends ListActivity {
	private static final int ACTIVITY_CREATE = 0;
	private static final int ACTIVITY_EDIT = 1;

	private static final int INSERT_ID = Menu.FIRST;
	private static final int DELETE_ID = Menu.FIRST + 1;

	private NotesDbAdapter mDbHelper;
	private Cursor mNotesCursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notepad_list);
		mDbHelper = new NotesDbAdapter(this);
		mDbHelper.open();
		fillData();
		registerForContextMenu(getListView());
		Button btn = (Button) findViewById(R.id.backpage);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),
						MainChapter14.class);
				if (mDbHelper != null) {
					mDbHelper.close();
				}
				startActivity(intent);

				finish();
			}
		});
	}

	private void fillData() {
		mNotesCursor = mDbHelper.fechtAllNotes();
		startManagingCursor(mNotesCursor);
		String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
		int[] to = new int[] { R.id.textNotesRow };
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.notes_row, mNotesCursor, from, to);
		setListAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, INSERT_ID, 0, R.string.menu_insert);
		return true;

	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case INSERT_ID:
			createNote();
			return true;
		}
		return super.onMenuItemSelected(featureId, item);
	}

	private void createNote() {
		Intent i = new Intent(this, NoteEdit.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case DELETE_ID:
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
					.getMenuInfo();
			mDbHelper.deleteNote(info.id);
			fillData();
			return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Cursor cursor = mNotesCursor;
		cursor.moveToPosition(position);
		Intent intent = new Intent(this, NoteEdit.class);
		intent.putExtra(NotesDbAdapter.KEY_ROWID, id);
		intent.putExtra(NotesDbAdapter.KEY_TITLE, cursor.getString(cursor
				.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
		intent.putExtra(NotesDbAdapter.KEY_BODY, cursor.getString(cursor
				.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
		startActivityForResult(intent, ACTIVITY_EDIT);
		cursor.close();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		Bundle bundle = intent.getExtras();
		switch (requestCode) {
		case ACTIVITY_CREATE:
			String title = bundle.getString(NotesDbAdapter.KEY_TITLE);
			String body = bundle.getString(NotesDbAdapter.KEY_BODY);
			mDbHelper.createNote(title, body);
			fillData();
			break;
		case ACTIVITY_EDIT:
			Long rowId = bundle.getLong(NotesDbAdapter.KEY_ROWID);
			if (rowId != null) {
				String title1 = bundle.getString(NotesDbAdapter.KEY_TITLE);
				String body1 = bundle.getString(NotesDbAdapter.KEY_BODY);
				mDbHelper.updateNote(rowId, title1, body1);
			}
			fillData();
			break;
		}
	}
}
